package com.study.talk.app.register.fragment;

import static androidx.navigation.fragment.FragmentKt.findNavController;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.reflect.TypeToken;
import com.study.talk.R;
import com.study.talk.app.base.BaseFragment;
import com.study.talk.databinding.FragmentSettingPasswordBinding;
import com.study.talk.entity.User;
import com.study.talk.entity.resp.ResultResponse;
import com.study.talk.entity.vo.RegisterUserVo;
import com.study.talk.net.OkHttp;
import com.study.talk.util.AppExecutor;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;
import com.study.talk.util.alert.AlertUtil;


public class SettingPasswordFragment extends BaseFragment {


    private FragmentSettingPasswordBinding fragmentSettingPasswordBinding;

    private SettingPasswordFragmentArgs settingPasswordFragmentArgs;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentSettingPasswordBinding = FragmentSettingPasswordBinding.inflate(inflater, container, false);

        settingPasswordFragmentArgs = SettingPasswordFragmentArgs.fromBundle(getArguments());

        return fragmentSettingPasswordBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fragmentSettingPasswordBinding.registerBtn.setOnClickListener(v -> {
            if (checkInput(fragmentSettingPasswordBinding.registerPassword, "请输入密码")) return;
            if (checkInput(fragmentSettingPasswordBinding.registerPasswordEnsure, "请输入确认密码"))
                return;

            String password = fragmentSettingPasswordBinding.registerPassword.getText().toString();
            String passwordEnsure = fragmentSettingPasswordBinding.registerPasswordEnsure.getText().toString();

            if (password.length() < 8) {
                ToastUtil.showToast(getContext(), "密码长度必须大于8位");
                return;
            }

            if (!password.equals(passwordEnsure)) {
                ToastUtil.showToast(getContext(), "两次密码不一致");
                return;
            }

            RegisterUserVo registerUserVo = new RegisterUserVo(settingPasswordFragmentArgs.getUsername(), password, settingPasswordFragmentArgs.getPhoneNumber());
            OkHttp.post("/user/register/simple", GsonInstance.instance.toJson(registerUserVo), res -> {
                ResultResponse<User> resultResponse = GsonInstance.instance.fromJson(res, new TypeToken<ResultResponse<User>>() {
                }.getType());
                if (resultResponse.getStatus() == 200) {
                    AppExecutor.getInstance().MAIN_HANDLER().post(() -> AlertUtil.show(getActivity(), "注册成功", "你的用户名为" + resultResponse.getData().getUsername() + "\n快去登录吧", b -> {
                        if (getActivity() != null) getActivity().finish();
                    }));
                } else {
                    AppExecutor.getInstance().MAIN_HANDLER().post(() -> AlertUtil.showEnsureAction(getActivity(), "注册失败", resultResponse.getMessage(), b -> {
                        findNavController(this).navigate(R.id.action_settingPasswordFragment_to_phoneNumberFragment);
                    }));
                }
            });
        });
    }
}